//
//  RecommendViewController.m
//  项目A_百思不得姐
//
//  Created by wudongyang on 16/5/25.
//  Copyright © 2016年 wudongyang. All rights reserved.
//

#import "RecommendViewController.h"
#import <AFNetworking.h>
#import "LeftRecommendModel.h"
#import "RecommendTableCell.h"
#import "RightRecommendTableViewCell.h"
#import "RightRecommendModel.h"
#import "LandingViewController.h"

static NSString *const leftTableId = @"reuse";
static NSString *const rightTableId = @"cell";
@interface RecommendViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *leftTableView;
@property(nonatomic, strong)NSMutableArray *leftAllArray;
@property(nonatomic, strong)UITableView *rightTableView;
@property(nonatomic, strong)NSMutableArray *rightAllArray;

@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.leftAllArray = [NSMutableArray array];
    self.rightAllArray = [NSMutableArray array];
    
    [self getData];
    
    
    
    self.title = @"推荐关注";
    self.view.backgroundColor = [UIColor whiteColor];
    // 创建左边的类型tableView
    self.leftTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 70, self.view.frame.size.height) style:UITableViewStylePlain];
    self.leftTableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xE8E8E8,0x343434);
    [self.view addSubview:self.leftTableView];
    self.leftTableView.dataSource = self;
    self.leftTableView.delegate = self;
    [self.leftTableView registerClass:[RecommendTableCell class] forCellReuseIdentifier:leftTableId];
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 创建右边的tableView
    self.rightTableView = [[UITableView alloc] initWithFrame:CGRectMake(70, 64, self.view.frame.size.width - 70, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.rightTableView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xE8E8E8,0x343434);
    self.rightTableView.dataSource = self;
    self.rightTableView.delegate = self;
    [self.view addSubview:self.rightTableView];
    [self.rightTableView registerClass:[RightRecommendTableViewCell class] forCellReuseIdentifier:rightTableId];
    self.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    
    
    // Do any additional setup after loading the view.
}
-(void)getData{
    //GET请求
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://api.budejie.com/api/api_open.php?a=category&appname=bs0315&asid=4968E5C2-2BDD-41EA-817D-38D64DF13441&c=subscribe&client=iphone&device=iPhone%204S&from=ios&jbk=0&mac=&market=&openudid=34cb777d7f354071132410ed6de5d4b4ad99f747&udid=&ver=4.2" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"0000000000%@",responseObject);
        NSMutableArray *array = [responseObject objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            LeftRecommendModel *model = [[LeftRecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.leftAllArray addObject:model];
        }
        [self.leftTableView reloadData];
        // 默认选中cell在第一行(在刷新TableView的下面写)
        [self.leftTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
        // 取出数组中第一个元素强制转换成LeftRecommendModel类型取出其中的ID
        [self DSData:((LeftRecommendModel *)self.leftAllArray[0]).ID];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];


}
-(void)DSData:(NSString *)ID{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:[NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?a=list&appname=bs0315&asid=986C8830-1671-4071-BB49-37A7E7F66615&c=subscribe&category_id=%@&client=iphone&device=ios%%20device&from=ios&jbk=0&mac=&market=&openudid=21c4a1f4b37d431985e8e05255fdfec2bcb43c5a&page=1&pagesize=50&udid=&uid=18333270&ver=4.1", ID] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
        [self.rightAllArray removeAllObjects];
        NSMutableArray *array = [responseObject objectForKey:@"list"];
        for (NSDictionary *dic in array) {
            RightRecommendModel *rightModel = [[RightRecommendModel alloc] init];
            [rightModel setValuesForKeysWithDictionary:dic];
            [self.rightAllArray addObject:rightModel];
        }
        [self.rightTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 左边TableView
    if (tableView == self.leftTableView) {
        return self.leftAllArray.count;
    }
    return self.rightAllArray.count;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        self.leftTableView.rowHeight = 50;
        RecommendTableCell *cell = [tableView dequeueReusableCellWithIdentifier:leftTableId];
        cell.leftModel = self.leftAllArray[indexPath.row];
        
        return cell;
    }
    self.rightTableView.rowHeight = 60;
    RightRecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:rightTableId];
    cell.rightModel = self.rightAllArray[indexPath.row];
    cell.selectionStyle = 0;
    // 关注按钮Block回调点击方法
    cell.GZBlock = ^(UIButton *button){
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *isLogin = [defaults objectForKey:@"isLogin"];
        if ([isLogin isEqualToString:@"0"]) {
            LandingViewController *landVC = [[LandingViewController alloc] init];
            [self presentViewController:landVC animated:YES completion:^{
                
                
            }];
        }else{
            if (button.selected == 0) {
                [button setImage:[UIImage imageNamed:@"qxgz"] forState:UIControlStateNormal];
            }else if (button.selected == 1){
            [button setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            }
            button.selected = !button.selected;
        
        }
        
    
    };
    return cell;
    
}
// 左TableView的点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTableView) {
        LeftRecommendModel *left = self.leftAllArray[indexPath.row];
        [self DSData:left.ID];
        return;
    }
    DSLog(@"右TableView点击方法");
   


}


@end
